package com.example.service.impl;

import com.example.constant.MessageConstant;
import com.example.constant.StatusConstant;
import com.example.dto.ProductPageQueryDTO;
import com.example.dto.ProductsDTO;
import com.example.entity.Products;
import com.example.exception.DeletionNotAllowedException;
import com.example.mapper.ProductsDao;
import com.example.result.PageResult;
import com.example.result.Result;
import com.example.service.ProductsService;
import com.example.variable.ProductAndStoreId;
import com.example.vo.ProductVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Products)表服务实现类
 *
 * @author makejava
 * @since 2024-03-12 16:46:47
 */
@Service("productsService")
public class ProductsServiceImpl implements ProductsService {
    @Resource
    private ProductsDao productsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Products queryById(Long id) {
        return this.productsDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param products 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Products> queryByPage(Products products, PageRequest pageRequest) {
        long total = this.productsDao.count(products);
        return new PageImpl<>(this.productsDao.queryAllByLimit(products, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param products 实例对象
     * @return 实例对象
     */
    @Override
    public Products insert(Products products) {
        this.productsDao.insert(products);
        return products;
    }

    @Override
    public Products insert(ProductsDTO productsDTO) {
        Products products = new Products();
        BeanUtils.copyProperties(productsDTO,products);
        products.setStatus(0);
        this.productsDao.insert(products);
        return products;
    }

    /**
     * 修改数据
     *
     * @param productsDTO 实例对象
     * @return 实例对象
     */
    @Override
    public Products update(ProductsDTO productsDTO) {
        Products products = new Products();
        BeanUtils.copyProperties(productsDTO,products);

        productsDao.update(products);
        return products;

    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.productsDao.deleteById(id) > 0;
    }

    @Override
    public PageResult pageQuery(ProductPageQueryDTO productPageQueryDTO) {
        PageHelper.startPage(productPageQueryDTO.getPage(),productPageQueryDTO.getPageSize());
        com.github.pagehelper.Page<Products> page = productsDao.pageQuery(productPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public Result delete(List<Long> ids) {
        //判断当前商品是否能够删除，是否存在起售商品
        for(Long id:ids){
            Products products = productsDao.queryById(id);
            if(products.getStatus() == StatusConstant.ENABLE)
                //起售中，不能删除
                return Result.error(MessageConstant.PRODUCT_ON_SALE);
//                throw new DeletionNotAllowedException(MessageConstant.PRODUCT_ON_SALE);
        }

        productsDao.deleteByIds(ids);
        return Result.success();
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Products products = Products.builder()
                .status(status)
                .id(id)
                .build();
        productsDao.update(products);
    }

    @Override
    public List<Products> list(Integer categoryId) {
        Products products = Products.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return productsDao.list(products);
    }

    @Override
    public List<ProductVO> listWithFlavor(ProductAndStoreId dish) {
        List<Products> productsList = productsDao.list(dish);

        List<ProductVO> productVOList = new ArrayList<>();

        for (Products d : productsList) {
            ProductVO dishVO = new ProductVO();
            BeanUtils.copyProperties(d,dishVO);

            productVOList.add(dishVO);
        }

        return productVOList;
    }
}
